<?php
	class database
	{
		private $sql;
		private $dt;
		// Ham xay dung khoi tao ket noi toi csdl
		public function database()
		{
			$db_host     = LT2N_HOST;
			$db_name     = LT2N_DBNAME;
			$db_username = LT2N_DBUSER;
			$db_password = LT2N_DBPASS;
			$conn = @mysql_connect("{$db_host}", "{$db_username}", "{$db_password}") or die("Khong the ket noi Database hay Database khong ton tai");
			@mysql_select_db("{$db_name}") or die("Kh�ng khong the database");
			@mysql_query("SET NAMES 'utf8'");
		}

		public function setSql($sql)
		{
			$this->sql = $sql;
		}
		public function getSql()
		{
			return $this->sql;
		}

		public function setDt($dt)
		{
			$this->dt = $dt;
		}
		public function getDt()
		{
			return $this->dt;
		}
		// Ham thuc thi cau lenh truy van
		public function query()
		{
			$this->dt = mysql_query($this->sql);
		}
		public function numrow($dt)
		{
			return mysql_num_rows($dt);
		}
		public function fetchrow()
		{
			return mysql_fetch_array($this->dt);
		}
		public function fetchArray()
		{
                        $list = array();
			if($this->numrow($this->dt) > 0)
			{
				while($row = $this->fetchrow())
				{
					$list[] = $row;
				}
			}
			return $list;
		}
		public function checkQuery()
		{
			if(mysql_affected_rows() > 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
		public function check($mes)
		{
			foreach($mes as $lm=>$rm)
			{
				$m1 = $lm;
				$m2 = $rm;
			}
			if($this->checkQuery()>0)
			{
				return $m1;
			}
			else
			{
				return $m2;
			}
		}
		public function getLatestid()
		{
			return mysql_insert_id();
		}
	}
?>
